package org.redmagic.client.location;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CacheLocation extends FileLocation {

	private static final Log LOG = LogFactory.getLog(CacheLocation.class);
	
	CacheLocation(String name, URI uri) {
		super(name, uri);
	}
	
	@Override
	public void finished(Properties resolvedProperties) {

		OutputStream os = null;
		
		try {
			os = new FileOutputStream(new File(this.getUri()));
			resolvedProperties.store(os, "RedMagic - Last known good configuration");
		} catch (Exception e) {
			LOG.warn("Unable to write properties to cache", e);
		} finally {
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {}
			}
		}
	}

	@Override
	public String toString() {
		return (this.getName() == null ? "CacheLocation" : this.getName()) + " [" + this.getUri() + "]";
	}
}
